package moviles.turnomovil.mundo;

import java.util.Calendar;
import java.util.Date;

import moviles.turnomovil.interfaz.TurnoMovilActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
 
public class TurnReceiver extends BroadcastReceiver
{
	
	
	public final static String BEGIN_SMS = "*/TurnoMovil/*\n";
	public final static String TURN = "*/TURNO:";
	public static String numPhoneUser;
	public static int turn;
	
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                str += msgs[i].getMessageBody().toString();
                // Verifica si es un mensaje asociado a TurnoMovil
                if(str.startsWith(BEGIN_SMS)){
                	if(str.contains(TURN)){
                		//Guarda el turno
                		str = str.replace(BEGIN_SMS, "");
                		str = str.replace("\n", "");
                		str = str.replace("*/*", "");
                		str = str.replace(TURN, "");
                		str = str.replace("/*", "");
                		str = str.trim();
                		
                		procesarTurno(str);
                		String[] datos = str.split(",");
                		context.startService( new Intent( context, AlertaService.class));
                		Toast.makeText(context, "Turno: "+datos[0], 2000).show();
                	}
                }
            }
        }                         
    }
    
	public void procesarTurno(String info){
		String[] datos = info.split(",");
		int turno = Integer.parseInt(datos[0]); 
		double lambda = Double.parseDouble(datos[1]); 
		double k = Double.parseDouble(datos[2]); 
		
		double kFact = 1;
		for(int i = 1 ; i <= k ; i++){
			kFact = kFact*i;
		}
		double probaPoisson = (Math.pow(Math.E, -lambda)*Math.pow(lambda, k))/kFact;
		probaPoisson = Math.rint(probaPoisson*10000)/10000; 
		int tiempoSegEspera = 1;
		
		double proba = 1;
		
		while (proba>0.1){
			proba = Math.pow((1-probaPoisson),tiempoSegEspera);
			tiempoSegEspera++;
		}
		
		Log.d("TURNO", tiempoSegEspera+"");
		Calendar calendario = Calendar.getInstance();
		calendario.setTime(new Date());
		calendario.add(Calendar.MINUTE, tiempoSegEspera);
		
		TurnoMovil.getInstance().setTiempoEstimado(calendario);
		TurnoMovil.getInstance().setTurno(turno);
	}
    
    public String getNumPhoneUser(){
    	return numPhoneUser;
    }

    

}